<template>
  <div>
    <el-card class="box-card">
      <el-row :gutter="20" class="my-3">
        <el-col :span="4" v-permission="['sys:message:delete']">
          <el-button @click="delAllSelection" type="button" :disabled="!multipleSelection.length"
            class="el-button handle-del el-button--primary ">
            <i class="el-icon-delete"></i><span>批量删除</span>
          </el-button>
        </el-col>
        <el-col :span="8">
          <div class="grid-content bg-purple">
            <el-input placeholder="请输入内容" v-model="searchContent" clearable>
              <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
            </el-input>
          </div>
        </el-col>
      </el-row>

      <!-- 留言列表区 -->
      <el-table border stripe :data="messageList" ref="multipleTable" @selection-change="handleSelectionChange"
        style="width: 100%">
        <el-table-column align="center" type="selection" show-overflow-tooltip></el-table-column>
        <el-table-column type="index" :index="indexSort" sortable align="center" label="#" width="50">
        </el-table-column>
        <el-table-column align="center" prop="name" label="用户名">
        </el-table-column>
        <el-table-column label="头像(查看大图)" align="center">
          <template #default="scope">
            <el-image class="table-td-headPortrait" :src="scope.row.headPortrait"
              :preview-src-list="[scope.row.headPortrait]"></el-image>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="phone" label="电话">
        </el-table-column>
        <el-table-column align="center" prop="email" label="邮箱">
        </el-table-column>
        <el-table-column align="center" width="150px" prop="message" label="留言内容">
        </el-table-column>
        <el-table-column align="center" prop="time" label="留言时间">
          <template slot-scope="scope">
            {{ formatDate(scope.row.time) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-tooltip v-permission="['sys:message:delete']" class="item" effect="dark" content="删除当前留言"
              placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" @click="deleteid(scope.row)" size="small"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <!-- 留言分页pc -->
      <div class="row ">
        <div class="block">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="currentPage" background :page-sizes="[4, 6, 8, 10]" :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
      </div>
    </el-card>
  </div>
</template>
<script>
import qs from "qs";
import formatDateTimes from "@/assets/Tools/date2.js";
export default {
  data() {
    return {
      // 分页
      total: 0,
      pageSize: 6,
      currentPage: 1,
      searchContent: "", // 搜索内容
      messageList: [],
      headPortrait: "",
      //删除列表
      delList: [],
      multipleSelection: [], //批量删除列表
    };
  },
  created() {
    this.getMessageList();
  },

  methods: {
    formatDate(val) {
      return formatDateTimes.formatDateTime(val);
    },
    async getMessageList() {
      const data = await this.$http.post(
        "/leavingMessage/message?" +
        qs.stringify({
          pageNum: this.currentPage,
          pageSize: this.pageSize,
        })
      );
      if (data.code == 200) {
        this.messageList = data.data.records;
        this.total = data.data.total;
      }
    },

    // 分页
    handleSizeChange(pageSize) {
      this.currentPage = 1;
      this.pageSize = pageSize;
      this.getMessageList();
    },
    // 跳转页数
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.getMessageList();
    },

    // 查询留言信息
    async search() {
      const data = await this.$http.post(
        "/admin/queryMessageByContent?" +
        qs.stringify({
          content: this.searchContent,
          pageNum: this.currentPage,
          pageSize: this.pageSize,
        })
      );
      if (data.code == 200) {
        this.messageList = data.data.records;
        this.total = data.data.total;
        this.searchContent = "";
      } else {
        this.$message.error("查询留言不存在");
      }
    },

    // 删出留言信息
    async deleteid(row) {
      const confirmresult = await this.$confirm(
        "此操作将永久删除该该留言,不可恢复， 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmresult != "confirm") {
        return this.$message.info("已取消删除！！");
      } else {
        const data = await this.$http.post("/admin/delMessage/" + row.id);
        if (data.code == 200) {
          this.$message.success("成功删除了 " + row.name + "用户的留言");
          this.getMessageList();
        } else if (data.code == 400) {
          this.$message.error(data.message);
        } else {
          this.$message.error("删除失败");
        }
        this.centerDialogVisible2 = false;
      }
    },
    // 监听哪些行数改变
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 批量删除按钮
    async delAllSelection() {
      const length = this.multipleSelection.length;
      let delList = [];
      for (let i = 0; i < length; i++) {
        delList.push(this.multipleSelection[i].id);
      }
      const data = await this.$http.post("/admin/delAllMessage", {
        list: delList,
      });
      if (data.code == 200) {
        let str = "";
        for (let i = 0; i < length; i++) {
          str += this.multipleSelection[i].name + "、";
        }
        this.$message.error(`成功删除了${str}` + "用户的留言");
        this.getMessageList();
      }
      this.multipleSelection = []; // 删除后清空列表
    },
    // 排序
    indexSort(index) {
      return (this.currentPage - 1) * this.pageSize + index + 1;
    },
  },
};
</script>

<style lang="less" scoped>
.table-td-headPortrait {
  width: 40px;
  height: 40px;
}
</style>
